import ika
import engine
import map
import controls

class IntroEvent(object):
    def __init__(self):
        self.done = False
        self.skip = False

    def Render(self):
        pass

    def Update(self):
        self.done = True

class FadeEvent(IntroEvent):
    def __init__(self, start_alpha, end_alpha, duration, after_delay=0):
        IntroEvent.__init__(self)
        self.alpha = start_alpha
        self.timer = 0
        self.start_alpha = start_alpha
        self.end_alpha = end_alpha
        self.duration = duration
        self.after_delay = after_delay
        self.skip_prepared = False
        
    def Update(self):
        if self.skip and not self.skip_prepared:
            self.start_alpha = self.alpha
            self.end_alpha = 0
            self.timer = 0
            self.duration = 30
            self.after_delay = 0
            self.skip_prepared = True
        self.timer += 1
        self.alpha = ((self.timer * (self.end_alpha - self.start_alpha) / self.duration) + self.start_alpha)
        if self.timer >= self.duration:
            self.alpha = self.end_alpha
            if self.timer >= self.duration + self.after_delay:
                self.done = True

class ImageFadeEvent(FadeEvent):
    def __init__(self, image, start_alpha, end_alpha, duration, after_delay=0):
        FadeEvent.__init__(self, start_alpha, end_alpha, duration, after_delay)
        self.image = image
        
    def Render(self):
        ika.Video.TintBlit(self.image, (ika.Video.xres - self.image.width) / 2, (ika.Video.yres - self.image.height) / 2, ika.RGB(255, 255, 255, self.alpha))
                
class ColorFadeEvent(FadeEvent):
    def __init__(self, color, start_alpha, end_alpha, duration, after_delay=0):
        FadeEvent.__init__(self, start_alpha, end_alpha, duration, after_delay)
        self.r, self.g, self.b, self.a = ika.GetRGB(color)
        
    def Render(self):
        ika.Video.DrawRect(0, 0, ika.Video.xres, ika.Video.yres, ika.RGB(self.r, self.g, self.b, int(self.a / 255.0 * self.alpha)), 1)

class Intro(object):
    def __init__(self):
        self.team_logo = ika.Image("graphics/allcaps.png")
        
        self.events = [
                        ImageFadeEvent(self.team_logo, 0, 255, 100, 200),
                        ImageFadeEvent(self.team_logo, 255, 0, 100, 250)
                      ]
        self.event_index = 0
        self.done = True

    def Open(self):
        engine.render_list.append(self.Render)
        engine.update_list.append(self.Update)
        engine.input_list.append(self.Input)
        self.event_index = 0
        engine.PlayMusic("music/Troupe_-_Pumpkin_Kingdom.mp3")
        self.skip = False
        self.done = False

    def Close(self):
        engine.render_list.remove(self.Render)
        engine.update_list.remove(self.Update)
        engine.input_list.remove(self.Input)
        self.done = True
        print "done intro"

    def Render(self):
        if self.event_index < len(self.events):
            self.events[self.event_index].Render()
    
    def Update(self):
        if self.event_index < len(self.events):
            e = self.events[self.event_index]
            e.Update()
            if e.done:
                if self.skip:
                    self.Close()
                else:
                    self.event_index += 1
        else:
            self.Close()
        
    def Input(self, held, pressed):
        if "attack" in pressed:
            self.skip = True
            if self.event_index < len(self.events):
                e = self.events[self.event_index]
                e.skip = True
        
class TitleMenu(object):
    def __init__(self):
        self.done = True
        
    def Open(self):
        engine.render_list.append(self.Render)
        engine.update_list.append(self.Update)
        engine.input_list.append(self.Input)
        self.done = False
    
    def Close(self):
        engine.render_list.remove(self.Render)
        engine.update_list.remove(self.Update)
        engine.input_list.remove(self.Input)
        self.done = True
        print "done title"
    
    def Render(self):
        f = engine.font
        f.CenterPrint(ika.Video.xres / 2, (ika.Video.yres - f.height) / 2 + f.height * 2, "Press " + controls.GetButtonName("attack") + " to Start")
        f.CenterPrint(ika.Video.xres / 2, ika.Video.yres - f.height - 10, "Better font, maybe? Needs logo.")
    
    def Update(self):
        pass
        
    def Input(self, held, pressed):
        if "attack" in pressed:
            # TODO: Put in cursor, check where it is here.
            # New game!
            if 1:
                map.map_engine.SwitchMap("placeholder.ika-map")
            self.Close()
    

intro = Intro()
title_menu = TitleMenu()

class Startup(object):
    def __init__(self):
        self.events = [intro, title_menu]
        self.event = None
        self.event_index = -1
        
    def Open(self):
        engine.update_list.append(self.Update)
        self.event_active = None
        self.event_index = -1
        
    def Close(self):
        engine.update_list.remove(self.Update)
        
    def Update(self):
        if not self.event or self.event.done:
            self.event_index += 1
            if self.event_index < len(self.events):
                self.event = self.events[self.event_index]
                self.event.Open()
            else:
                self.Close()
            
startup = Startup()